/**
 * 
 */
package vue.popup;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import model.session.CNFUse;
import model.solver.ESolver;
import model.solver.SolverFactory;
import controller.ISessionManager;
import controller.solver.MiniSat;
import controller.solver.RSat;
import controller.solver.Solver;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGSolverDialog extends JDialog implements ActionListener {

	private static final long				serialVersionUID			= 1L;
	public static final String				TITLE						= "Solver execution dialog";
	public static final String				RUN_MESSAGE					= "RUN !!";
	public static final String				COMBO_TITLE					= "Choose solver";
	public static final int					PARAMETER_DIALOG_POSITION	= 1;

	private ISessionManager					manager;
	private CNFUse							cnf;

	// private Set<ESolver> availableDialogSolver = new HashSet<ESolver>();
	private Map<Solver, IGDialogParameter>	dialogsParameters			= new HashMap<Solver, IGDialogParameter>();

	private Solver							selected					= null;

	private JComboBox						comboBox;
	private JButton							run;
	private JPanel							panel;

	public IGSolverDialog(ISessionManager manager, CNFUse cnf, Window owner) {
		super(owner, TITLE);
		// TODO Auto-generated constructor stub
		this.manager = manager;
		this.cnf = cnf;
		initDialogsParamater();

		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		setContentPane(panel);


		// comboBox
		comboBox = new JComboBox(dialogsParameters.keySet().toArray());
		comboBox.setBorder(BorderFactory.createTitledBorder(COMBO_TITLE));
		comboBox.addActionListener(this);

		panel.add(comboBox);

		// solver
		panel.add(dialogsParameters.get(selected));
		comboBox.setSelectedItem(selected);

		// bouton
		run = new JButton(RUN_MESSAGE);
		// run.setEnabled(false);
		run.setAlignmentX(JButton.RIGHT_ALIGNMENT);
		run.addActionListener(this);

		panel.add(run);

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		pack();
		System.out.println("crerr");
	}

	private void initDialogsParamater() {
		// TODO a tester
		SolverFactory fact = manager.getSolverFactory();
		for (ESolver s : manager.getAvailableSolver())
		{
			Solver solver = null;
			IGDialogParameter dialog = null;
			try
			{
				switch (s) {
				case MINISAT:
					MiniSat mini = fact.createMiniSatSolver();
					if (mini == null)
						break;
					solver = mini;
					dialog = new IGMinisatDialogParameter(mini.getMiniSatParameter());
					break;
				case RSAT:
					RSat rsat = fact.createRSatSolver();
					if (rsat == null)
						break;
					solver = rsat;
					dialog = new IGRSatDialogParameter(rsat.getRSatParameter());
					break;
				default:
					solver = null;
				}
			} catch (Exception e)
			{
				e.printStackTrace();
				solver = null;
			}
			if (solver == null || dialog == null)
				continue;

			dialogsParameters.put(solver, dialog);
			selected = solver;
		}

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == comboBox)
		{// change the solverParameter
			selected = (Solver) comboBox.getSelectedItem();
			panel.remove(PARAMETER_DIALOG_POSITION);
			panel.add(dialogsParameters.get(selected), PARAMETER_DIALOG_POSITION);
			pack();
			return;
		}

		if (e.getSource() == run)
		{
			System.out.println("confirme run");
			if (dialogsParameters.get(selected).checkAndSetParameter())
			{
				System.out.println("execute solver");
				manager.executeSolver(selected, cnf);
				dispose();
			}
			return;
		}

	}
}
